package apdate

import activity.DriversActivity
import android.content.Context
import android.content.Intent
import android.content.res.Resources
import android.graphics.BitmapFactory
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import com.wd.common.bean.RimRecommendBean
import com.wd.rim.R
import java.text.SimpleDateFormat
import java.util.Date

class SquareApdate(private var context: Context,private var list:RimRecommendBean):RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if(viewType==1){
            val inflate = LayoutInflater.from(context).inflate(R.layout.item_sq, parent, false)
            return ViewHolder(inflate)
        }else if(viewType==3){
            val inflate = LayoutInflater.from(context).inflate(R.layout.item_three, parent, false)
            return ThreeViewHolder(inflate)
        }else if(viewType==2){
            val inflate = LayoutInflater.from(context).inflate(R.layout.item_video, parent, false)
            return VideoViewHolder(inflate)
        }else{
            val inflate = LayoutInflater.from(context).inflate(R.layout.item_sq, parent, false)
            return ViewHolder(inflate)
        }

    }

    override fun getItemCount(): Int {
        return list.result.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val viewType = holder.itemViewType
        if(holder is ViewHolder){
            var viewHolder=(holder)as SquareApdate.ViewHolder
            viewHolder.name.setText(list.result[position].title)
            viewHolder.nickName.setText(list.result[position].nickName)
            viewHolder.Time.text="${SimpleDateFormat("yyyy-MM-dd").format(Date(list.result[position].releaseTime))}"
            viewHolder.Pub.setText("发布于"+list.result[position].publishLocation)
            Glide.with(context)
                .load(list.result[position].mediaUrl)
                .error(R.mipmap.aa1)
                .placeholder(R.mipmap.aa1)
                .into(viewHolder.img)
        }else if(holder is ThreeViewHolder){
          var threeView=(holder) as SquareApdate.ThreeViewHolder
            threeView.name2.setText(list.result[position].title)
            threeView.nickName2.setText(list.result[position].nickName)
            threeView.Time2.text="${SimpleDateFormat("yyyy-MM-dd").format(Date(list.result[position].releaseTime))}"
            threeView.Pub2.setText("发布于"+list.result[position].publishLocation)
            Glide.with(context)//第一张图片
                .load(list.result[position].mediaUrl)
                .error(R.mipmap.aa1)
                .placeholder(R.mipmap.aa1)
                .into(threeView.img1)

            Glide.with(context)//第一张图片
                .load(list.result[position].mediaUrl)
                .error(R.mipmap.aa1)
                .placeholder(R.mipmap.aa1)
                .into(threeView.img2)

            Glide.with(context)//第一张图片
                .load(list.result[position].mediaUrl)
                .error(R.mipmap.aa1)
                .placeholder(R.mipmap.aa1)
                .into(threeView.img3)
        }else if(holder is VideoViewHolder){
            var videoView=(holder) as SquareApdate.VideoViewHolder
            videoView.name3.setText(list.result[position].title)
            videoView.video.setUp(list.result[position].mediaUrl,false,"")
            videoView.nickName3.setText(list.result[position].nickName)
            videoView.Time3.text="${SimpleDateFormat("yyyy-MM-dd").format(Date(list.result[position].releaseTime))}"
            videoView.Pub3.setText("发布于"+list.result[position].publishLocation)
        }

        holder.itemView.setOnClickListener {
            val intent = Intent()
            intent.putExtra("name",list.result[position].nickName)
            intent.setClass(context,DriversActivity::class.java)
            context.startActivity(intent)
        }

    }
    class ViewHolder(view: View):RecyclerView.ViewHolder(view){
        val name:TextView
        val img:ImageView
        val nickName:TextView
        val Time:TextView
        val Pub:TextView
        init {
           name = view.findViewById<TextView>(R.id.name)
             img = view.findViewById<ImageView>(R.id.image_id)
                nickName = view.findViewById<TextView>(R.id.nickName)
             Time = view.findViewById<TextView>(R.id.Tim)
             Pub = view.findViewById<TextView>(R.id.Pub)

        }
    }

    class ThreeViewHolder(view: View):RecyclerView.ViewHolder(view){
        val name2:TextView
        val img1:ImageView
        val img2:ImageView
        val img3:ImageView
        val nickName2:TextView
        val Time2:TextView
        val Pub2:TextView
        init {
            name2 = view.findViewById<TextView>(R.id.name2)
            img1 = view.findViewById<ImageView>(R.id.img1)
            img2 = view.findViewById<ImageView>(R.id.img2)
            img3 = view.findViewById<ImageView>(R.id.img3)
            nickName2 = view.findViewById<TextView>(R.id.nickName2)
            Time2 = view.findViewById<TextView>(R.id.Tim2)
            Pub2 = view.findViewById<TextView>(R.id.Pub2)

        }
    }

    class VideoViewHolder(view: View):RecyclerView.ViewHolder(view){
        val name3:TextView
        val video:StandardGSYVideoPlayer
        val nickName3:TextView
        val Time3:TextView
        val Pub3:TextView
        init {
            name3 = view.findViewById<TextView>(R.id.name3)
             video = view.findViewById<StandardGSYVideoPlayer>(R.id.videoId)
            nickName3 = view.findViewById<TextView>(R.id.nickName3)
            Time3 = view.findViewById<TextView>(R.id.Tim3)
            Pub3 = view.findViewById<TextView>(R.id.Pub3)

        }
    }

    override fun getItemViewType(position: Int): Int {//多条目
        if(list.result[position].type==1){
            if(list.result[position].mediaUrl==null||list.result[position].mediaUrl?.split(",")?.size!!>1){
                return 3
            }else{
                return 1
            }
        }else{
            return 2
        }

    }


}